package com.sjq.service.impl;

import com.sjq.mapper.RoleMapper;
import com.sjq.mapper.UserMapper;
import com.sjq.mapper.UserRoleMapper;
import com.sjq.model.entity.User;
import com.sjq.model.request.GetUserRequest;
import com.sjq.model.request.UserDto;
import com.sjq.model.request.UserRequest;
import com.sjq.service.IUserService;
import com.sjq.util.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * @author dolyw.com
 * @date 2021/12/9 15:45
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserDto> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 查找用户状态
     * @param account
     * @return
     */
    @Override
    public int getStatusByAccount(String account){
        return userMapper.getStatusByAccount(account);
    }

    /**
     * 插入用户信息
     * @param user
     */
    @Override
    public int insertUser(User user){
        return userMapper.insertUser(user);
    }


    /**
     * 查询需要审核的用户
     * @return
     */
    @Override
    public List<User> getUnAuthorizedUserList(){
        return userMapper.getUnAuthorizedUserList();
    }

    /**
     * 更新用户状态
     */
    @Override
    public int updateStatus(User user){
        Long status = user.getStatus();
        if(status == 1){
            EmailUtil.sendEmail(user.getEmail(), "尊敬的"+user.getUsername()+"用户,恭喜注册成功");
        }
        else{
            EmailUtil.sendEmail(user.getEmail(), "尊敬的"+user.getUsername()+"用户,你提供的凭证不准确，审核未通过，请重新提供凭证注册！");
        }
        return userMapper.updateStatus(user);
    }


    /**
     * 加入用户角色表
     * @param userId
     * @param roleId
     */
    @Override
    public int insertUserRole(Long userId, Long roleId){
        return userRoleMapper.insertUserRole(userId,roleId);
    }



    /**
     * 通过id查找用户信息
     * @param id
     * @return
     */
    @Override
    public UserDto selectById(Integer id){
        return userMapper.selectById(id);
    }

    /**
     * 插入专家分类表
     * @param userId
     * @param classificationId
     */
    @Override
    public int insertExpertClassification(Long userId, Long classificationId){
        return userMapper.insertExpertClassification(userId,classificationId);
    }

    @Override
    public List<String> getRoleById(Long userId){
        return userMapper.getRoleById( userId);
    }

    @Override
    public List<UserRequest> selectUser(GetUserRequest baseDto){
        return userMapper.selectUser( baseDto);
    }
}
